package com.srd.phone.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.adapter.BillHistoryAdapter;
import com.srd.phone.bean.BillDetail;
import com.srd.phone.mvp.presenters.UtilPresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.LogUtils;

/**
 * 账单详情
 */
public class BillDetailYActivity extends BaseActivity implements  View.OnClickListener {

    private TextView tv_title_name, tv_return_money, tv_money_all, tv_return_num, tv_interest_rate;

    private RelativeLayout rl_layout_0, rl_layout_1, rl_layout_2;
    private LinearLayout ll_layout_1, ll_layout_2;
    private ImageView iv_right_1, iv_right_2;
    private ListView mListView = null;
    private BillHistoryAdapter adapter = null;
    private UtilPresenter utilPresenter;
    private int id = 45;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill_detail_y);

        initView();
        initData();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void initView() {
        tv_common_title.setText("账单详情");

        rl_layout_0 = (RelativeLayout)findViewById(R.id.rl_layout_0);
        rl_layout_0.setOnClickListener(this);
        rl_layout_1 = (RelativeLayout)findViewById(R.id.rl_layout_1);
        rl_layout_1.setOnClickListener(this);
        rl_layout_2 = (RelativeLayout)findViewById(R.id.rl_layout_2);
        rl_layout_2.setOnClickListener(this);

        ll_layout_1 = (LinearLayout)findViewById(R.id.ll_layout_1);
        ll_layout_2 = (LinearLayout)findViewById(R.id.ll_layout_2);

        iv_right_1 = (ImageView)findViewById(R.id.iv_right_1);
        iv_right_2 = (ImageView)findViewById(R.id.iv_right_2);

        tv_title_name = (TextView)findViewById(R.id.tv_title_name);
        tv_return_money = (TextView)findViewById(R.id.tv_return_money);
        tv_money_all = (TextView)findViewById(R.id.tv_money_all);
        tv_return_num = (TextView)findViewById(R.id.tv_return_num);
        tv_interest_rate = (TextView)findViewById(R.id.tv_interest_rate);

        mListView = (ListView)findViewById(R.id.listview);
        mListView.setFocusable(false);
        adapter = new BillHistoryAdapter(this);
        mListView.setAdapter(adapter);
    }

    public void initData() {
        showWaitingDialog();
        presenter = new UtilPresenter(requestQueue, this);
        utilPresenter = (UtilPresenter)presenter;
        setUrlArrs(Constants.ACCOUNT_DETAIL);

        getData();
    }

    private void getData(){
        utilPresenter.accountDetail(Constants.ACCOUNT_DETAIL, id);
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        LogUtils.i(BillDetailYActivity.class, tag + "   " + data);
        cancelWaitingDialog();
        if(Constants.ACCOUNT_DETAIL.equals(tag)){
            BillDetail billDetail = BillDetail.parseData(data);
            refreshView(billDetail);
        }
    }

    private void refreshView(BillDetail billDetail){
        tv_title_name.setText(billDetail.nm);
        tv_return_money.setText(billDetail.moneys + "元");
        tv_money_all.setText(billDetail.mCount + "元");
        tv_return_num.setText(billDetail.term + "");
        tv_interest_rate.setText(billDetail.anyield + "%");  // 年利率
        adapter.setData(billDetail.list);
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
        cancelWaitingDialog();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.rl_layout_0:  // 债权转让
                gotoActivity(NoReturnMoneyActivity.class);
                break;
            case R.id.rl_layout_1:
                if(ll_layout_1.getVisibility() == View.GONE){
                    ll_layout_1.setVisibility(View.VISIBLE);
                    iv_right_1.setBackgroundResource(R.mipmap.icon_down);
                }else{
                    ll_layout_1.setVisibility(View.GONE);
                    iv_right_1.setBackgroundResource(R.mipmap.icon_right);
                }
                break;
            case R.id.rl_layout_2:
                if(ll_layout_2.getVisibility() == View.GONE){
                    ll_layout_2.setVisibility(View.VISIBLE);
                    iv_right_2.setBackgroundResource(R.mipmap.icon_down);
                }else{
                    ll_layout_2.setVisibility(View.GONE);
                    iv_right_2.setBackgroundResource(R.mipmap.icon_right);
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}